//
//  AppDelegate.m
//  25-GCD
//
//  Created by dllo on 16/8/17.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
//    调研GCD, 明天会找同学分享(有想讲的也可以私聊我, 如果没有我就点名了)。
//    总写代码会累, 适当研究些东西。
//    
//    调研需要提交的成果
//    1.调研后的代码一份
//    2.调研过程中所查阅的资料合集(格式任意)
    
//    GCD：是Grand Central Dispatch缩写，是Apple公司封装的一个用于进行并发程序设计的一个API，它以C语言为基础，将线程的操作抽象为对队列的操作。
//    Dispatch Queue：Dispatch Queue是GCD中用到的队列类型，有串行队列、并发队列、主队列等。
//    
//    Dispatch Queue类型
//    串行队列：任务按被加入到队列中的顺序依次执行，每次只从队中取出一个任务执行。
//    并发队列：同时执行一个或多个任务，但任务仍然是按它们被添加到队列中的顺序开始的。
//    主队列：是一个全局串行队列，它在应用程序的主线程上执行任务。因为它运行在您的应用程序的主线程上，主队列经常被用来作为应用程序的一个关键的同步点。
    
//    队列与线程的关系
//    主队列中的任务都是在主线程中执行的，而串行队列与并发队列在Apple的官方文档Dispatch Queue中都有这样一句描述， The currently executing task runs on a distinct thread that is managed by the dispatch queue.，我理解为串行队列和并发队列在执行任务时都会新开线程。
    
    
    
    
    
    
//    后台执行
//    dispatch_async(dispatch_get_global_queue(0, 0), ^{
//        
//    });
////    主线程执行
//    dispatch_async(dispatch_get_main_queue(), ^{
//        
//    });
////    一次性执行
//    static dispatch_once_t onceToken;
//    dispatch_once(&onceToken, ^{
//        
//    });
////    延迟2秒执行
//    double delayInSeconds = 2.0;
//    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
//    dispatch_after(popTime, dispatch_get_main_queue(), ^{
//        
//    });
//    
////    自定义dispatch_queue_t
//    dispatch_queue_t urls_queue = dispatch_queue_create("blog.devtang", NULL);
//    dispatch_async(urls_queue, ^{
//        
//    });
//    
////    合并汇总结果
//    dispatch_group_t group = dispatch_group_create();
//    dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{
////       并行执行的线程一
//    });
//    dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{
////        并行执行的线程二
//    });
//    dispatch_group_notify(group, dispatch_get_global_queue(0, 0), ^{
////        汇总结果
//    });
//    
//    
//    
//    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//        NSURL *url = [NSURL URLWithString:@"https://www.baidu.com"];
//        NSError *error = nil;
//        NSString *data = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
//        if (data != nil) {
//            dispatch_async(dispatch_get_main_queue(), ^{
//                NSLog(@"call back, the data is: %@",data);
//            });
//        } else {
//            NSLog(@"error when download:%@",error);
//        }
//    });
    
    
    
    
    
    
    
    
    
    
    
    
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
//    [self beginBackgroundUpdateTask];
////    在这里加上你需要长久运行的代码
//    [self endBackgroundUpdateTask];
    
    
    
    
}

//- (void)beginBackgroundUpdateTask
//{
//    self.backgroundUpdateTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
//        [self endBackgroundUpdateTask];
//    }];
//}
//
//- (void)endBackgroundUpdateTask
//{
//    [[UIApplication sharedApplication] endBackgroundTask:_backgroundUpdateTask];
//    _backgroundUpdateTask = UIBackgroundTaskInvalid;
//}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
